package com.aaa.aspect;

import com.aaa.annotation.Auth;
import com.aaa.utils.JWTUtil;
import com.aaa.vo.Result;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @program: aaa-education-parent
 * @description:
 * @author: 闫克起2
 * @create: 2024-03-07 09:22
 **/
@Component //该类有spring创建
@Aspect //表示该类为切面类   //权限校验
public class AuthAspect {
    //1. 定义切断--凡是使用了@Auth注解的都使用该切面
    @Pointcut(value = "@annotation(com.aaa.annotation.Auth)")
    private void pointcut(){}
    @Autowired
    private HttpServletRequest request;

    //2. 通知类型 环绕通知  日志
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        //获取方法切面 切入的方法类对象
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method method = signature.getMethod();
        //获取方法的@Auth注解对象
        Auth annotation = method.getAnnotation(Auth.class);
        //获取注解的value属性值
        String value=annotation.value();
        //1.获取token.
        String token = request.getHeader("token");
        //2.获取token中权限内容
        List<String> permission = JWTUtil.getPermission(token);
        if(permission.contains(value)){
            Object o=point.proceed();//执行切面切入的方法
            return o;
        }
        //返回权限不足
        Result result=new Result(4003,"权限不足",null);
        return result;
    }

}













